{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def count_events(year):\n",
    "    metadata_dir = \"/storeSSD/eracah/data/metadata/\"\n",
    "    coord_keys = [\"xmin\", \"xmax\", \"ymin\", \"ymax\"]\n",
    "    d ={\"us-ar\":0, \"etc\":0, \"tc\":0}\n",
    "    for weather_type in d.keys(): \n",
    "        if weather_type == 'us-ar':\n",
    "            labeldf = pd.read_csv(join(metadata_dir, 'ar_labels.csv'))\n",
    "            labeldf = labeldf.ix[(labeldf.year==year)]\n",
    "            d[weather_type] = len(labeldf)\n",
    "#             tmplabeldf=labeldf.ix[ (labeldf.month==ts.month) & (labeldf.day==ts.day) & (labeldf.year==ts.year) ].copy()\n",
    "        else:\n",
    "            labeldf = pd.read_csv(join(metadata_dir, '_'.join([str(year),weather_type, 'labels.csv'])))\n",
    "#             tmplabeldf=labeldf.ix[ (labeldf.month==ts.month) & (labeldf.day==ts.day) ].copy()\n",
    "            if weather_type == \"etc\":\n",
    "                d[weather_type] = len(labeldf)\n",
    "            else:\n",
    "                d[\"td\"] = len(labeldf[labeldf[\"str_category\"] == \"tropical_depression\"])\n",
    "                d[\"tc\"] = len(labeldf[labeldf[\"str_category\"] == \"tropical_cyclone\"])\n",
    "            \n",
    "    return d\n",
    "\n",
    "def count_events_md(year, last_md = (3,16)):\n",
    "    lm, ld = last_md\n",
    "    metadata_dir = \"/storeSSD/eracah/data/metadata/\"\n",
    "    coord_keys = [\"xmin\", \"xmax\", \"ymin\", \"ymax\"]\n",
    "    d ={\"us-ar\":0, \"etc\":0, \"tc\":0}\n",
    "    for weather_type in d.keys(): \n",
    "        if weather_type == 'us-ar':\n",
    "            labeldf = pd.read_csv(join(metadata_dir, 'ar_labels.csv'))\n",
    "            for i in range(1,lm):\n",
    "                labeldf = labeldf.ix[(labeldf.year==year) & (labeldf.month == i)]\n",
    "                d[weather_type] += len(labeldf)\n",
    "            for i in range(1,ld+1):\n",
    "                labeldf_end = labeldf.ix[(labeldf.year==year) & (labeldf.month ==lm) & (labeldf.day == i)]\n",
    "                d[weather_type] += len(labeldf_end)\n",
    "           \n",
    "        else:\n",
    "            labeldf = pd.read_csv(join(metadata_dir, '_'.join([str(year),weather_type, 'labels.csv'])))\n",
    "            if weather_type == \"etc\":\n",
    "                for i in range(1,lm):\n",
    "                    labeldfi = labeldf.ix[(labeldf.year==year) & (labeldf.month == i)]\n",
    "                    d[weather_type] += len(labeldfi)\n",
    "                for i in range(1,ld+1):\n",
    "                    labeldfi = labeldf.ix[(labeldf.year==year) & (labeldf.month ==lm) & (labeldf.day == i)]\n",
    "                    d[weather_type] += len(labeldfi)\n",
    "            else:\n",
    "                d[\"td\"] = 0\n",
    "                for i in range(1,lm):\n",
    "                    labeldfi = labeldf.ix[(labeldf.year==year) & (labeldf.month == i)]\n",
    "                    d[\"td\"] += len(labeldfi[labeldfi[\"str_category\"] == \"tropical_depression\"])\n",
    "                    d[\"tc\"] += len(labeldfi[labeldfi[\"str_category\"] == \"tropical_cyclone\"])\n",
    "                for i in range(1,ld+1):\n",
    "                    labeldfi = labeldf.ix[(labeldf.year==year) & (labeldf.month ==lm) & (labeldf.day == i)]\n",
    "                    d[\"td\"] += len(labeldfi[labeldfi[\"str_category\"] == \"tropical_depression\"])\n",
    "                    d[\"tc\"] += len(labeldfi[labeldfi[\"str_category\"] == \"tropical_cyclone\"])\n",
    "              \n",
    "    return d\n",
    "\n",
    "def get_percents(event_dict):\n",
    "    tot = sum(event_dict.values())\n",
    "    new_d = {k: float(v)/ tot for k,v in event_dict.iteritems()}\n",
    "    return new_d"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
